//
//  MineViewController.m
//  ZhiHuiLiJiang
//
//  Created by Work on 2018/11/12.
//  Copyright © 2018年 王雅鹏. All rights reserved.
//我的VC

#import "MineViewController.h"
#import "MineTopView.h"//我的头部View
#import "FixViewController.h"//设置VC
#import "BYLoginVC.h"//登录的VC

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) MineTopView *topView;
@property (nonatomic ,strong) UITableView *tabelView;

@end
@implementation MineViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self addSubViewClick];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
    if (self.tabBarController.tabBar.isHidden) {
        self.tabBarController.tabBar.hidden = NO;
    }
    [self loadTopView];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
-(void)loadTopView{
    if ([[SaveCacheData loadDataStrWithKey:loginState] isEqualToString:@"YES"]) {//已登录
        _topView.nameLabel.hidden = YES;
        _topView.image = [UIImage imageNamed:@"Heard_icon"];
        _topView.heardImaegView.hidden = YES;
    }else{
        _topView.nameLabel.hidden = NO;
        _topView.backgroundColor = RGBA(226, 226, 226, 1);
        _topView.image = [UIImage imageNamed:@""];
        _topView.heardImaegView.hidden = NO;
        _topView.heardImaegView.image = [UIImage imageNamed:@"mineIcon_HeadIcon"];
    }
}
- (void)setupUI{
    //头部View
    _topView = [[MineTopView alloc]initWithFrame:CGRectMake(0, 0, BY_ScreenWidth, BY_Height(445))];
    [self.view addSubview:_topView];
    
    //tableView
    _tabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, BY_Height(445), BY_ScreenWidth, BY_ScreenHeight - BY_Height(445) - BY_Bot_H - 44)];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    _tabelView.rowHeight = BY_Height(98);
    _tabelView.tableFooterView =[[UIView alloc]init];
     [self.view addSubview:self.tabelView];
}
- (void)addSubViewClick{
    BY_kWeakSelf(ws);
    _topView.clickUserCentBtn = ^{//跳转到个人中心
        [ws jumpUserCent];
    };
    _topView.clickSettingBtn = ^{//跳转到设置
        [ws jumpSettingVC];
    };
    _topView.clickHeardImgLogin = ^{//点击头像登录
        [ws jumpLoginVC];
    };
}

#pragma mark- tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID=@"cellID";
    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    if (indexPath.row ==0) {
        cell.textLabel.text =@"我的关注";
        cell.imageView.image =[UIImage imageNamed:@"mineIcon_follow"];
        cell.detailTextLabel.text = @">";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text =@"我的足迹";
        cell.imageView.image =[UIImage imageNamed:@"mineIcon_footprint"];
        cell.detailTextLabel.text = @">";
    }
    if (indexPath.row == 2) {
        cell.textLabel.text =@"我的订单";
        cell.imageView.image =[UIImage imageNamed:@"我的订单"];
        cell.detailTextLabel.text = @">";
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 0) {//跳转我的关注
        if ([[SaveCacheData loadDataStrWithKey:loginState] isEqualToString:@"YES"]) {
            for (BYSetModel *models in APPDELEGATE.SetDataArray) {
                if ([models.setting_name isEqualToString:@"personal-fav-list"]) {
                    NSString *urlStr = models.setting_value;
                    NSString *paramStr = [NSString stringWithFormat:@"userid=%@&token=%@",[SaveCacheData loadDataStrWithKey:currentUserudid] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserudid],[SaveCacheData loadDataStrWithKey:currentUserToken] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserToken]];
                    NSString *webURL= @"";
                    if ([urlStr rangeOfString:@"?"].location == NSNotFound) {//没有?参数
                        webURL = [NSString stringWithFormat:@"%@?%@",urlStr,paramStr];
                    } else {//有参数
                        webURL = [NSString stringWithFormat:@"%@&%@",urlStr,paramStr];
                    }
                    BYWebVC *VC = [[BYWebVC alloc]init];
                    VC.url = webURL;
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }
        }else{
            BYLoginVC *loginVC = [[BYLoginVC alloc]init];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:loginVC] animated:YES completion:nil];
            loginVC.loginComplete = ^{//登录后的跳转等操作
                for (BYSetModel *models in APPDELEGATE.SetDataArray) {
                    if ([models.setting_name isEqualToString:@"personal-fav-list"]) {
                        NSString *urlStr = models.setting_value;
                        NSString *paramStr = [NSString stringWithFormat:@"userid=%@&token=%@",[SaveCacheData loadDataStrWithKey:currentUserudid] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserudid],[SaveCacheData loadDataStrWithKey:currentUserToken] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserToken]];
                        NSString *webURL= @"";
                        if ([urlStr rangeOfString:@"?"].location == NSNotFound) {//没有?参数
                            webURL = [NSString stringWithFormat:@"%@?%@",urlStr,paramStr];
                        } else {//有参数
                            webURL = [NSString stringWithFormat:@"%@&%@",urlStr,paramStr];
                        }
                        BYWebVC *VC = [[BYWebVC alloc]init];
                        VC.url = webURL;
                        [self.navigationController pushViewController:VC animated:YES];
                    }
                }
            };
        }
    }
    if (indexPath.row == 1) {//跳转我的足迹
        if ([[SaveCacheData loadDataStrWithKey:loginState] isEqualToString:@"YES"]) {
            for (BYSetModel *models in APPDELEGATE.SetDataArray) {
                if ([models.setting_name isEqualToString:@"personal-histroy-list"]) {
                    NSString *urlStr = models.setting_value;
                    NSString *paramStr = [NSString stringWithFormat:@"userid=%@&token=%@",[SaveCacheData loadDataStrWithKey:currentUserudid] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserudid],[SaveCacheData loadDataStrWithKey:currentUserToken] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserToken]];
                    NSString *webURL= @"";
                    if ([urlStr rangeOfString:@"?"].location == NSNotFound) {//没有?参数
                        webURL = [NSString stringWithFormat:@"%@?%@",urlStr,paramStr];
                    } else {//有参数
                        webURL = [NSString stringWithFormat:@"%@&%@",urlStr,paramStr];
                    }
                    BYWebVC *VC = [[BYWebVC alloc]init];
                    VC.url = webURL;
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }
        }else{
            BYLoginVC *loginVC = [[BYLoginVC alloc]init];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:loginVC] animated:YES completion:nil];
            loginVC.loginComplete = ^{//登录后的跳转等操作
                for (BYSetModel *models in APPDELEGATE.SetDataArray) {
                    if ([models.setting_name isEqualToString:@"personal-histroy-list"]) {
                        NSString *urlStr = models.setting_value;
                        NSString *paramStr = [NSString stringWithFormat:@"userid=%@&token=%@",[SaveCacheData loadDataStrWithKey:currentUserudid] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserudid],[SaveCacheData loadDataStrWithKey:currentUserToken] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserToken]];
                        NSString *webURL= @"";
                        if ([urlStr rangeOfString:@"?"].location == NSNotFound) {//没有?参数
                            webURL = [NSString stringWithFormat:@"%@?%@",urlStr,paramStr];
                        } else {//有参数
                            webURL = [NSString stringWithFormat:@"%@&%@",urlStr,paramStr];
                        }
                        BYWebVC *VC = [[BYWebVC alloc]init];
                        VC.url = webURL;
                        [self.navigationController pushViewController:VC animated:YES];
                    }
                }
            };
        }
    }
    
    if (indexPath.row == 2) {//跳转我的订单
        if ([[SaveCacheData loadDataStrWithKey:loginState] isEqualToString:@"YES"]) {
            for (BYSetModel *models in APPDELEGATE.SetDataArray) {
                if ([models.setting_name isEqualToString:@"sys-mybooking-url"]) {
                    NSString *urlStr = models.setting_value;
                    NSString *paramStr = [NSString stringWithFormat:@"userid=%@&token=%@",[SaveCacheData loadDataStrWithKey:currentUserudid] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserudid],[SaveCacheData loadDataStrWithKey:currentUserToken] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserToken]];
                    NSString *webURL= @"";
                    if ([urlStr rangeOfString:@"?"].location == NSNotFound) {//没有?参数
                        webURL = [NSString stringWithFormat:@"%@?%@",urlStr,paramStr];
                    } else {//有参数
                        webURL = [NSString stringWithFormat:@"%@&%@",urlStr,paramStr];
                    }
                    BYWebVC *VC = [[BYWebVC alloc]init];
                    VC.url = webURL;
                    [self.navigationController pushViewController:VC animated:YES];
                }
            }
        }else{
            BYLoginVC *loginVC = [[BYLoginVC alloc]init];
            [self presentViewController:[[UINavigationController alloc] initWithRootViewController:loginVC] animated:YES completion:nil];
            loginVC.loginComplete = ^{//登录后的跳转等操作
                for (BYSetModel *models in APPDELEGATE.SetDataArray) {
                    if ([models.setting_name isEqualToString:@"sys-mybooking-url"]) {

                        NSString *urlStr = models.setting_value;
                        NSString *paramStr = [NSString stringWithFormat:@"userid=%@&token=%@",[SaveCacheData loadDataStrWithKey:currentUserudid] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserudid],[SaveCacheData loadDataStrWithKey:currentUserToken] ==nil?@"":[SaveCacheData loadDataStrWithKey:currentUserToken]];
                        NSString *webURL= @"";
                        if ([urlStr rangeOfString:@"?"].location == NSNotFound) {//没有?参数
                            webURL = [NSString stringWithFormat:@"%@?%@",urlStr,paramStr];
                        } else {//有参数
                            webURL = [NSString stringWithFormat:@"%@&%@",urlStr,paramStr];
                        }
                        BYWebVC *VC = [[BYWebVC alloc]init];
                        VC.url = webURL;
                        [self.navigationController pushViewController:VC animated:YES];
                    }
                }
            };
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"内存警告");
}
- (void)jumpUserCent{
    NSLog(@"点击了个人中心");
}
-(void)jumpSettingVC{//跳转到设置页面
    FixViewController  *settingVC = [[FixViewController alloc]init];
    [self.navigationController pushViewController:settingVC animated:YES];
}
- (void)jumpLoginVC{//跳转到登录VC
    if (![[SaveCacheData loadDataStrWithKey:loginState] isEqualToString:@"YES"]) {
        BYLoginVC *loginVC = [[BYLoginVC alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }}


@end
